package com.hzlj.position.dbsync.shanxi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.dict.annotation.DictConvert;
import com.hzlj.position.config.common.model.JzJbxxDeviceBind;
import com.hzlj.position.config.dao.JzJbxxDeviceBindDao;
import com.hzlj.position.dbsync.shanxi.common.dto.tSqjzDevice.TSqjzDeviceQueryParamDTO;
import com.hzlj.position.dbsync.shanxi.common.dto.tSqjzDevice.TSqjzDeviceQueryResultDTO;
import com.hzlj.position.dbsync.shanxi.config.DataSourceContextHolder;
import com.hzlj.position.dbsync.shanxi.dao.TSqjzDeviceDao;
import com.hzlj.position.dbsync.shanxi.service.TSqjzDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;
import static com.hzlj.position.dbsync.shanxi.constant.DataSource.SHANXI;

/**
 * 定位设备表(TSqjzDevice)表服务接口
 *
 * @author lifh
 * @date 2023-07-17 15:52:40
 */
@Slf4j
@Service
@DS(SHANXI)
public class TSqjzDeviceServiceImpl implements TSqjzDeviceService {
    @Resource
    private TSqjzDeviceDao tSqjzDeviceDao;
    @Resource
    private DataSourceContextHolder dataSourceContextHolder;
    @Resource
    private JzJbxxDeviceBindDao jzJbxxDeviceBindDao;

    /**
     * 查询列表：不分页
     *
     * @param dto 查询条件
     * @return 对象列表
     */
    @Override
    @DictConvert
    public List<TSqjzDeviceQueryResultDTO> listTSqjzDevice(TSqjzDeviceQueryParamDTO dto) {
        return this.tSqjzDeviceDao.listTSqjzDevice(dto);
    }

    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<TSqjzDeviceQueryResultDTO> pageTSqjzDevice(TSqjzDeviceQueryParamDTO dto, PageParamDTO page) {
        return this.tSqjzDeviceDao.pageTSqjzDevice(dto, page);
    }

    @Override
    public long sync() {
        List<TSqjzDeviceQueryResultDTO> list = this.listTSqjzDevice(new TSqjzDeviceQueryParamDTO());
        if (ObjectUtil.isEmpty(list)) {
            return 0L;
        }
        List<JzJbxxDeviceBind> binds;
        try {
            dataSourceContextHolder.dsSwitch(null);
            //同步数据
            binds = list.stream().map(e -> {
                JzJbxxDeviceBind bind = new JzJbxxDeviceBind();
                bind.setId(POSITION_ID_GEN.nextIdStr());
                bind.setJzId(e.getRymcid().toString());
                bind.setDeviceCode(e.getDevice());
                bind.setChannelType("2");//固定移动
                bind.setChannel("2");
                bind.setOnline(false);
                bind.setLocateInterval(Integer.parseInt(e.getInterval()));
                bind.setLocateTimes(null);
                bind.setFirstLocateTime(null);
                bind.setValid(true);
                bind.setUpdateTime(new Date());
                bind.setCreateTime(new Date());
                return bind;
            }).collect(Collectors.toList());

            jzJbxxDeviceBindDao.saveBatch(binds);

        } finally {
            dataSourceContextHolder.clear();
        }
        return binds.size();
    }
}
